package com.fjwt.gz.mch.ctrl.proxy;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AwardCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/proxy/sendCouponActivity")
public class SendCouponUserProxyController extends CommonCtrl {

    @Autowired
    private AwardCouponService awardCouponService;

    /**
     * 机构扫码派券给商家用户
     *
     * @return
     */
    @PostMapping("/sendToMchUser")
    @Transactional
    public ApiRes sendToMchUser() {
        Long activityId = getValLong("activityId");
        Long agentUserId = getCurrentUser().getUserId();
        Long agentFansId = getCurrentUser().getFansId();
        JSONObject receiptContent = getVal("receiptContent", JSONObject.class);
        String appId = request.getHeader("appid");
        Long fansId = getValLong("fansId");
        Long userId = getValLong("userId");
        log.info("机构扫码派券给商家用户进入信息,活动id：{}，机构用户userid：{},发券对象userid:{},回执信息：{}", activityId, agentUserId, userId, JSONUtil.toJsonStr(receiptContent));
        awardCouponService.sendToMchUser(activityId, agentUserId, userId, fansId, receiptContent, appId, agentFansId);

        return ApiRes.ok();
    }
}
